
#!/bin/bash

# 设置颜色
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[1;34m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# 提示用户输入的函数
prompt_input() {
    read -p "$1: " input
    echo "$input"
}

# 创建 kibana.yml 配置文件
create_kibana_yml() {
    echo -e "${YELLOW}正在创建 kibana.yml 配置文件...${NC}"

    # 获取用户输入
    ES_HOST=$(prompt_input "请输入 Elasticsearch 主机地址 (默认 localhost)")
    ES_PORT=$(prompt_input "请输入 Elasticsearch 端口号 (默认 9200)")
    ES_USERNAME=$(prompt_input "请输入 Elasticsearch 用户名 (默认 elastic)")
    ES_PASSWORD=$(prompt_input "请输入 Elasticsearch 密码 (默认 changeme)")
    KIBANA_USERNAME=$(prompt_input "请输入 Kibana 用户名 (默认 kibana)")
    KIBANA_PASSWORD=$(prompt_input "请输入 Kibana 密码 (默认 kibanapass)")
    KIBANA_PORT=$(prompt_input "请输入 Kibana 端口号 (默认 5601)")
    KIBANA_PATH_DATA=$(prompt_input "请输入 Kibana 数据目录 (默认 /var/lib/kibana)")
    KIBANA_PATH_LOGS=$(prompt_input "请输入 Kibana 日志目录 (默认 /var/log/kibana)")
    KIBANA_PATH_CONF=$(prompt_input "请输入 Kibana 配置目录 (默认 /etc/kibana)")
    KIBANA_PATH_PLUGINS=$(prompt_input "请输入 Kibana 插件目录 (默认 /usr/share/kibana/plugins)")
    KIBANA_PATH_TMP=$(prompt_input "请输入 Kibana 临时目录 (默认 /tmp)")

    # 设置默认值，如果用户没有输入则使用默认值
    ES_HOST=${ES_HOST:-"localhost"}
    ES_PORT=${ES_PORT:-"9200"}
    ES_USERNAME=${ES_USERNAME:-"elastic"}
    ES_PASSWORD=${ES_PASSWORD:-"changeme"}
    KIBANA_USERNAME=${KIBANA_USERNAME:-"kibana"}
    KIBANA_PASSWORD=${KIBANA_PASSWORD:-"kibanapass"}
    KIBANA_PORT=${KIBANA_PORT:-"5601"}
    KIBANA_PATH_DATA=${KIBANA_PATH_DATA:-"/var/lib/kibana"}
    KIBANA_PATH_LOGS=${KIBANA_PATH_LOGS:-"/var/log/kibana"}
    KIBANA_PATH_CONF=${KIBANA_PATH_CONF:-"/etc/kibana"}
    KIBANA_PATH_PLUGINS=${KIBANA_PATH_PLUGINS:-"/usr/share/kibana/plugins"}
    KIBANA_PATH_TMP=${KIBANA_PATH_TMP:-"/tmp"}

    # 检查并创建配置文件路径
    if [ ! -d "$KIBANA_PATH_CONF" ]; then
        echo -e "${RED}$KIBANA_PATH_CONF 目录不存在，正在创建目录...${NC}"
        mkdir -p $KIBANA_PATH_CONF
    fi

    # 删除可能存在的错误目录，确保配置文件为文件而不是目录
    if [ -d "$KIBANA_PATH_CONF/kibana.yml" ]; then
        echo -e "${RED}错误：$KIBANA_PATH_CONF/kibana.yml 是一个目录，正在删除该目录...${NC}"
        rm -r $KIBANA_PATH_CONF/kibana.yml
    fi

    # 创建新的配置文件
    echo -e "${YELLOW}正在写入配置文件...${NC}"
    cat <<EOL > $KIBANA_PATH_CONF/kibana.yml
# Kibana 启动时的绑定地址
server.host: "0.0.0.0"

# Elasticsearch 的主机地址
elasticsearch.hosts: ["http://$ES_HOST:$ES_PORT"]

# Elasticsearch 身份验证信息
elasticsearch.username: "$ES_USERNAME"
elasticsearch.password: "$ES_PASSWORD"

# Kibana 用户名和密码
kibana.username: "$KIBANA_USERNAME"
kibana.password: "$KIBANA_PASSWORD"

# 数据目录、日志目录、配置目录、插件目录和临时目录
path.data: "$KIBANA_PATH_DATA"
path.logs: "$KIBANA_PATH_LOGS"
path.conf: "$KIBANA_PATH_CONF"
path.plugins: "$KIBANA_PATH_PLUGINS"
path.tmp: "$KIBANA_PATH_TMP"

# 启用/禁用 X-Pack 安全
xpack.security.enabled: true

# 日志设置
logging.dest: /var/log/kibana.log

# 启动时的日志级别
logging.level: "info"

# Elasticsearch 请求超时时间
elasticsearch.requestTimeout: 30000

# Kibana 监听的端口
server.port: $KIBANA_PORT
EOL

    echo -e "${GREEN}kibana.yml 配置文件已成功创建并写入配置目录！${NC}"
}

# 安装 Kibana 函数
install_kibana() {
    echo -e "${YELLOW}开始安装 Kibana...${NC}"

    # 创建配置文件
    create_kibana_yml

    # 启动 Kibana 容器
    echo -e "${YELLOW}正在启动 Kibana 容器...${NC}"
    docker run --name kibana \
        -e ELASTICSEARCH_HOSTS=$ES_HOST \
        -e ELASTICSEARCH_USERNAME=$ES_USERNAME \
        -e ELASTICSEARCH_PASSWORD=$ES_PASSWORD \
        -e KIBANA_USERNAME=$KIBANA_USERNAME \
        -e KIBANA_PASSWORD=$KIBANA_PASSWORD \
        -e KIBANA_PATH_DATA=$KIBANA_PATH_DATA \
        -e KIBANA_PATH_LOGS=$KIBANA_PATH_LOGS \
        -e KIBANA_PATH_CONF=$KIBANA_PATH_CONF \
        -e KIBANA_PATH_PLUGINS=$KIBANA_PATH_PLUGINS \
        -e KIBANA_PATH_TMP=$KIBANA_PATH_TMP \
        -p $KIBANA_PORT:5601 \
        -v $KIBANA_PATH_DATA:/usr/share/kibana/data \
        -v $KIBANA_PATH_LOGS:/usr/share/kibana/log \
        -v $KIBANA_PATH_CONF:/usr/share/kibana/config \
        -v $KIBANA_PATH_PLUGINS:/usr/share/kibana/plugins \
        -v $KIBANA_PATH_TMP:/tmp \
        -d docker.elastic.co/kibana/kibana:7.17.7

    echo -e "${GREEN}Kibana 容器已成功启动！${NC}"
    echo -e "${GREEN}访问 Kibana: http://$(hostname -I | awk '{print $1}'):$KIBANA_PORT${NC}"
}

# 卸载 Kibana 函数
uninstall_kibana() {
    echo -e "${YELLOW}正在卸载 Kibana...${NC}"

    # 停止并删除容器
    docker stop kibana && docker rm kibana

    # 清理数据目录和日志目录
    echo -e "${YELLOW}清理数据和日志目录...${NC}"
    rm -rf $KIBANA_PATH_DATA
    rm -rf $KIBANA_PATH_LOGS
    rm -rf $KIBANA_PATH_CONF

    echo -e "${GREEN}Kibana 已成功卸载，数据目录已清理！${NC}"
}

# 显示环境变量信息
show_env() {
    echo -e "${YELLOW}当前环境变量信息:${NC}"
    echo "ES_HOST = $ES_HOST"
    echo "ES_PORT = $ES_PORT"
    echo "ES_USERNAME = $ES_USERNAME"
    echo "ES_PASSWORD = $ES_PASSWORD"
    echo "KIBANA_USERNAME = $KIBANA_USERNAME"
    echo "KIBANA_PASSWORD = $KIBANA_PASSWORD"
    echo "KIBANA_PORT = $KIBANA_PORT"
    echo "KIBANA_PATH_DATA = $KIBANA_PATH_DATA"
    echo "KIBANA_PATH_LOGS = $KIBANA_PATH_LOGS"
    echo "KIBANA_PATH_CONF = $KIBANA_PATH_CONF"
    echo "KIBANA_PATH_PLUGINS = $KIBANA_PATH_PLUGINS"
    echo "KIBANA_PATH_TMP = $KIBANA_PATH_TMP"
}

# 主菜单
show_menu() {
    clear
    echo -e "${GREEN}====================================${NC}"
    echo -e "${YELLOW}Kibana 安装与管理脚本${NC}"
    echo -e "${GREEN}====================================${NC}"
    echo -e "${YELLOW}当前系统: $(uname -s)$(uname -r)${NC}"
    echo -e "${YELLOW}兼容的系统: Linux, macOS, Windows（WSL）${NC}"
    echo -e "${GREEN}====================================${NC}"

    # 显示菜单选项
    echo -e "${YELLOW}1. 安装 Kibana${NC}"
    echo -e "${YELLOW}2. 卸载 Kibana${NC}"
    echo -e "${YELLOW}3. 显示环境变量信息${NC}"
    echo -e "${YELLOW}4. 退出${NC}"

    read -p "请输入选项 [1-4]: " choice

    case $choice in
        1)
            install_kibana
            ;;
        2)
            uninstall_kibana
            ;;
        3)
            show_env
            ;;
        4)
            exit 0
            ;;
        *)
            echo -e "${RED}无效选择，请重新输入！${NC}"
            show_menu
            ;;
    esac
}

# 运行菜单
show_menu

